package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.ReportDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author likepei
 * @Date 2020/2/21 10:52
 * @Version v1.0
 * @Description 图表的业务实现类
 */
@Service
@Slf4j
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public List<Map<String, Object>> findmemberSexCount() {

        String sex1 = "1";
        String sex2 = "2";
        Integer count1 = memberDao.selectMemberSexCount(sex1);
        Integer count2 = memberDao.selectMemberSexCount(sex2);

        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "男");
        map1.put("value", count1);


        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "女");
        map2.put("value", count2);

        Integer count3 = memberDao.selectMemberNoneSexCount();
        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", "未知");
        map3.put("value", count3);




        List<Map<String, Object>> mapList = new ArrayList<>();
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);




        return mapList;


    }

    @Override
    public List<Map<String, Object>> findMemberAgeCount() {

        List<Map<String, Object>> mapList = new ArrayList<>();
        // 调用Dao 查询用户出生日期
        List<String> dates = memberDao.selectBirthday();
        log.info("dates===========:{}", dates);
        // 定义计数器 计算符合年龄段会员的数量
        int count1 = 0;
        int count2 = 0;
        int count3 = 0;
        int count4 = 0;
        Map<String, Object> mapAgeandCount1 = new HashMap<>();
        mapAgeandCount1.put("name", "0-18");
        Map<String, Object> mapAgeandCount2 = new HashMap<>();
        mapAgeandCount2.put("name", "18-30");
        Map<String, Object> mapAgeandCount3 = new HashMap<>();
        mapAgeandCount3.put("name", "30-45");
        Map<String, Object> mapAgeandCount4 = new HashMap<>();
        mapAgeandCount4.put("name", "45以上");



        Calendar calendar = Calendar.getInstance();
        Date date1 = calendar.getTime();
        DateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 得到每个会员的出生的年月日(1997-11-01)
        String memberDate = dataFormat.format(date1);
        String yearBirthday = memberDate.substring(0, 4);
        log.info("yearBirthday===========:{}", yearBirthday);
        int year = Integer.parseInt(yearBirthday);
        log.info("year=====22222======:{}", year);

        for (String date : dates) {
            if (null != date && date.length()>0) {
                Integer memberYear = calculateAgeByBirthday(date);
                log.info("memberYear=====222======:{}",memberYear);
                // 得到会员的年龄
                int old = year - memberYear;
                log.info("memberYear======222=====:{}", old);
                // 判断会员的年龄属于哪个年龄段
                if (old <= 18) {
                    count1++;
                    // mapAgeandCount1.put("name", "0-18");
                    mapAgeandCount1.put("value", count1);
                    mapList.add(mapAgeandCount1);
                }
                if (old > 18 && old <= 30) {
                    count2++;
                    // mapAgeandCount2.put("name", "18-30");
                    mapAgeandCount2.put("value", count2);
                    mapList.add(mapAgeandCount2);
                }
                if (old > 30 && old <= 45) {
                    count3++;
                    //  mapAgeandCount3.put("name", "30-45");
                    mapAgeandCount3.put("value", count3);
                    mapList.add(mapAgeandCount3);

                }
                if(old > 45){
                    // 全都是45以上的
                    count4++;
                    //mapAgeandCount4.put("name", "45以上");
                    mapAgeandCount4.put("value", count4);
                    mapList.add(mapAgeandCount4);
                }
            }
            log.info("mapList===========:{}", mapList);
            //return mapList;

        }

        Map<String, Object> mapAgeandCount5 = new HashMap<>();

        Integer count5 = memberDao.selectNoneBirthday();
        mapAgeandCount5.put("name", "未知");
        mapAgeandCount5.put("value", count5);
        mapList.add(mapAgeandCount5);

        log.info("mapList======222=====:{}", mapList);
        return mapList;
    }

    private Integer calculateAgeByBirthday(String date) {
        String yearBirthday = date.substring(0, 4);
        log.info("yearBirthday===========:{}", yearBirthday);
        // 得到每个会员的出生的年(1997)
        // 将String 转换成 int
        int year = Integer.parseInt(yearBirthday);
        log.info("yearBirthday===========:{}",year);
        return year;
    }

    /**
     * 根据用户在日历中选择的日期查询
     * @param months
     * @return
     */
    @Override
    public List<Integer> getMemberReportBySelect(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();

        //遍历获取到 每一个月份
        for (String month : months) {
            // month ===>  2020.04
            month = month+".31"; // 拼接后的日期 格式            2020.04.31

            //调用dao, 得到指定月份的会员累计数量
            Integer count = reportDao.findMemberCountBeforeDate( month );
            memberCount.add(count);
        }

        return memberCount;
    }

    /**
     * 获取指定月份列表的 会员数量
     * @param monthList
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> monthList) {
        List<Integer> memberCount = new ArrayList<>();

        //遍历获取到 每一个月份
        for (String month : monthList) {
            // month ===>  2020.04
            month = month+".31"; // 拼接后的日期 格式            2020.04.31

            //调用dao, 得到指定月份的会员累计数量
            Integer count = reportDao.findMemberCountBeforeDate( month );
            memberCount.add(count);
        }

        return memberCount;
    }

    /**
     * 套餐占比数据
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return reportDao.findSetmealCount();
    }

    /**
     * 获取运营统计数据
     * Map数据格式：
     *	    reportDate -> String 				// 报表日期
     *      todayNewMember -> Integer			// 今日新增会员
     *      thisWeekNewMember -> Integer		// 本周新增会员
     *      thisMonthNewMember -> Integer		// 本月新增会员
     *      totalMember -> Integer		    	// 总会员
     *      todayOrderNumber -> Integer			// 今日预约人数
     *      thisWeekOrderNumber -> Integer		// 本周预约人数
     *      thisMonthOrderNumber -> Integer		// 本月预约人数
     *      todayVisitsNumber -> Integer		// 今日到诊人数
     *      thisWeekVisitsNumber -> Integer		// 本周到诊人数
     *      thisMonthVisitsNumber -> Integer	// 本月到诊人数
     *      hotSetmeal -> List<Map<String,Object>>	// 热门套餐
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        try {
            //获得今天日期
            //String today = "2020-02-22";
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周第一天日期(周一)
            //String thisWeekMonday = "2020-02-17";
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

            //获得本月第一天日期
            //String thisMonthFirst = "2020-02-01";
            String thisMonthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>today = " + today);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>thisWeekMonday = " + thisWeekMonday);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>thisMonthFirst = " + thisMonthFirst);

            //获取与会员有关数据 (今日、本周、本月、总人数)
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(thisMonthFirst);
            Integer totalMember = memberDao.findMemberTotalCount();

            //获取与预约有关数据 (今日、本周、本月)
            Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
            Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(thisWeekMonday);
            Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(thisMonthFirst);

            //获得到诊相关数据 (今日、本周、本月)
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountByAfterDate(thisWeekMonday);
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountByAfterDate(thisMonthFirst);

            //获得套餐热门数据
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            //封装数据到map集合
            Map<String, Object> resultMap = new HashMap<>();
            //封装报表日期
            resultMap.put("reportDate", today);
            //会员有关数据 (今日、本周、本月、总人数)
            resultMap.put("todayNewMember", todayNewMember);
            resultMap.put("thisWeekNewMember", thisWeekNewMember);
            resultMap.put("thisMonthNewMember", thisMonthNewMember);
            resultMap.put("totalMember", totalMember);
            //获取与预约有关数据 (今日、本周、本月)
            resultMap.put("todayOrderNumber", todayOrderNumber);
            resultMap.put("thisWeekOrderNumber", thisWeekOrderNumber);
            resultMap.put("thisMonthOrderNumber", thisMonthOrderNumber);
            //到诊相关数据 (今日、本周、本月)
            resultMap.put("todayVisitsNumber", todayVisitsNumber);
            resultMap.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            resultMap.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
            //套餐热门数据
            resultMap.put("hotSetmeal", hotSetmeal);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

}
